package com.expdatacloud.controller.itrn;


import com.expdatacloud.entity.YtWorkOrderDetail;
import com.expdatacloud.entity.log.HandleLogBean;
import com.expdatacloud.service.LogService;
import com.expdatacloud.service.YtWorkOrderDetailService;
import com.expdatacloud.utils.GetListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/fileImport")
public class ImportExcelController {
    /**
     * 自定义每次插入的条数,如果插入的数据非常多的话
     * 比如插入百万条数据，那么分组插入可以节省很多时间
     * 并且可以防止插入时间过长二报错
     * 其实最方便的还是用多线程来插入（有时间再写）
     */
    private static final int OPENID_PHONE_NUM = 800;

    @Autowired
    private YtWorkOrderDetailService ytWorkOrderDetailService;

    @Autowired
    private LogService logService;


    /**
     * 获取到people集合
     * @return
     */
    @RequestMapping("/insert")
    public int insertAll(){
        //现获取people集合，自定义的一个从Excel获取集合的方法。
        List<YtWorkOrderDetail> people= GetListUtil.getAllListUtil();
        int listSize=people.size();
        int toIndex=OPENID_PHONE_NUM;
        int a;
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;

        Date b = new Date();

        for(int i = 0;i<people.size();i+=OPENID_PHONE_NUM){
            if(i+OPENID_PHONE_NUM>listSize){//作用为toIndex最后没有800条数据则剩余几条newList中就装几条
                toIndex=listSize-i;
            }
            List<YtWorkOrderDetail> newPMeSendToPhoneInfo = people.subList(i,i+toIndex);

            Date d = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss ");

            System.out.println("插入第 "+i+" 至 "+(i+toIndex)+" 条,开始时间："+ sdf.format(d));

            //批量插入
            a =  ytWorkOrderDetailService.insertList(newPMeSendToPhoneInfo);
//            logService.info(new HandleLogBean("导入工单明细","测试","",",","","插入第 "+i+" 至 "+(i+toIndex)+" 条,成功!"));

            Date d1 = new Date();
            System.out.println("插入第 "+i+" 至 "+(i+toIndex)+" 条,成功 "+ a +" 条,结束时间："+ sdf.format(d1)+", 用时:"+ (d.getTime() - d1.getTime())% nd % nh % nm / ns +"秒");
        }

//        logService.info(new HandleLogBean("导入工单明细","测试","",",","","插入完成,共"+people.size()+"条记录!"));

        Date b1 = new Date();
        System.out.println("插入完成,共"+people.size()+"条记录, 共用时:"+ (b.getTime() - b1.getTime())% nd % nh % nm / ns +"秒");
        return -1;
    }

}
